package solution;
import hex.HexMove;
import hex.HexState;
import java.util.ArrayList;
public class Defense {
	public static Point getNextMove(HexState hs)
	{
		Point p = null;
		ArrayList<Point> oppMoves = getOpposingMoves(hs);
		ArrayList<Point> prevMoves = getOurMoves(hs);
		switch(HexPlayer.turnNum)
		{
		case 0:
				p = new Point(oppMoves.get(0).x -1, oppMoves.get(0).y);
				if(hs.moveOK(new HexMove(Point.toHexMove(p))))
					return p;
				else
					return new Point(p.x + 2, p.y);
		case 1:
				int dist = Point.getDist(oppMoves.get(0), oppMoves.get(1));
				p = new Point(oppMoves.get(1).x -1, oppMoves.get(1).y);
				if(dist == 2){
					Point ropes[] = Rope.getBothPathsBetweenPoints(oppMoves.get(0), oppMoves.get(1));
						if(ropes[0].equals(prevMoves.get(0))) //This section checks if we can block their expo
							return ropes[1];
						else if(ropes[1].equals((prevMoves).get(0)))
							return ropes[0];
							else
								if(hs.moveOK(new HexMove(Point.toHexMove(p))))
									return p;//Plays left side
								else
									return new Point(p.x + 2 , p.y);//Plays opposite side if can't play left
				} 
				if(dist < 2)
				{
					if(oppMoves.get(1).x > oppMoves.get(0).x)
					{
						Point pUp = new Point(prevMoves.get(0).x, prevMoves.get(0).y + 1);
						if(hs.moveOK(new HexMove(Point.toHexMove(pUp))))
							return pUp;
						else
							return new Point(pUp.x, pUp.y - 2);
					}
					else
					{
						Point temp = oppMoves.get(1);						
						if(hs.moveOK(new HexMove(temp.x - 1, temp.y)))
							return new Point(temp.x - 1, temp.y);
						else
							return new Point(temp.x + 1, temp.y);
					}
				}
				if(dist > 2)
				{
					Point temp = prevMoves.get(0);
					if(hs.moveOK(new HexMove(temp.x, temp.y + 1)))
						return new Point(temp.x, temp.y + 1);
					else
						return new Point(temp.x, temp.y - 1);
				}
				break;	
		default:
				Point tempTest = prevMoves.get(1);
				int c = 1;
				while(!hs.moveOK(new HexMove(tempTest.x, tempTest.y + c)))
				{
					if(tempTest.y + c > 10)
						c = -10;
					else
						c++;
				}
				return tempTest;
		}
		return p;
	}
	private static ArrayList<Point> getOpposingMoves(HexState hS){
	       ArrayList<Point> moves = new ArrayList<Point>();
	       for (int m = 0; m < hS.board.length; m++){
	           for (int n = 0; n < hS.board.length; n++) {
	               if (hS.board[m][n] == HexState.homeSym)
	                   moves.add(new Point(m, n));
	           }
	       }
	       return moves;
    }
	private static ArrayList<Point> getOurMoves(HexState hS){
		ArrayList<Point> moves = new ArrayList<Point>();
	       for (int m = 0; m < hS.board.length; m++){
	           for (int n = 0; n < hS.board.length; n++) {
	               if (hS.board[m][n] == HexState.awaySym)
	                   moves.add(new Point(m, n));
	           }
	       }
	       return moves;
 }
	
	}
